# Contributor: Borys Zhukov <mp5@mp5.im>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: omni <omni+alpine@hack.org>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=ocaml5
pkgver=5.2.0
pkgrel=1
pkgdesc="Main implementation of the Caml programming language"
url="https://ocaml.org/"
# loongarch64: no upstream support, unofficial port provided as ocaml5-loongarch
arch="all !loongarch64"
license="LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception"
depends="$pkgname-runtime=$pkgver-r$pkgrel gcc"
checkdepends="parallel"
subpackages="
	$pkgname-doc
	$pkgname-ocamldoc
	$pkgname-compiler-libs:_compiler_libs
	$pkgname-runtime
	"
source="https://caml.inria.fr/pub/distrib/ocaml-${pkgver%.*}/ocaml-$pkgver.tar.xz"
builddir="$srcdir/ocaml-$pkgver"

# Current native compiler support in 5.2.x
case "$CARCH" in
x86_64|aarch64|riscv64|s390x|ppc64le)
	_native_compiler=true
	;;
*)
	_native_compiler=false
	;;
esac

build() {
	local make_target extra_conf_opts
	if $_native_compiler; then
		make_target="world.opt"
		extra_conf_opts="
			--enable-native-compiler
			--enable-native-toplevel
			--disable-installing-bytecode-programs
			"
		msg "Building native compiler for $CARCH"
	else
		make_target="world"
		extra_conf_opts="
			--disable-native-compiler
			--disable-native-toplevel
			--enable-installing-bytecode-programs
			"
		msg "Building bytecode compiler for $CARCH"
	fi

	want_check && extra_conf_opts="
		$extra_conf_opts
		--enable-ocamltest
		"

	./configure \
		--prefix /usr \
		--bindir /usr/bin \
		--libdir /usr/lib/ocaml \
		--mandir /usr/share/man \
		--docdir /usr/share/doc/$pkgname \
		--disable-installing-source-artifacts \
		$extra_conf_opts \
		CC="${CC:-gcc}"
	make $make_target
}

check() {
	make ocamltest
	make -C testsuite parallel
}

package() {
	make DESTDIR="$pkgdir" install
}

ocamldoc() {
	pkgdesc="$pkgdesc (documentation generator)"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/bin/ocamldoc*
	amove usr/lib/ocaml/ocamldoc
}

_compiler_libs() {
	pkgdesc="$pkgdesc (compiler libraries)"
	depends="$pkgname=$pkgver-r$pkgrel"
	provides="$pkgname-dev=$pkgver-r$pkgrel"

	amove usr/lib/ocaml/compiler-libs
}

runtime() {
	pkgdesc="$pkgdesc (runtime environment)"
	depends=

	amove usr/bin/ocamlrun*
	amove usr/lib/ocaml/ld.conf

	local i; for i in \
		'*.cmo' \
		'*.cmi' \
		'*.cma' \
		'stublibs' \
		'threads/*.cmi' \
		'threads/*.cma'
	do
		amove "usr/lib/ocaml/$i"
	done
}

sha512sums="
6c2376777f0ec567fc414d534cba65e5f2120f7c766de25f36bbf80260e882f5d1c94c2ce8e12f2b2982097089c6e8f928f26dbc0b559a0463e7dcdd8baf20fe  ocaml-5.2.0.tar.xz
"
